package org.ims.inv.service.conference.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.ims.inv.dao.conference.DAOConferenceEvent;
import org.ims.inv.model.conference.ModelConference;
import org.ims.inv.model.conference.ModelConferenceEvent;
import org.ims.inv.service.conference.ServiceConferenceEvent;
import org.ims.inv.web.util.ContextUtil;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;

/**
 * Service: 会议事件
 * 
 * @author x36zhao
 *
 */
public class ServiceConferenceEventImpl
extends ServiceGenericImpl<ModelConferenceEvent> implements ServiceConferenceEvent
{
	
	/**
	 * The repository of conference event
	 */
	private DAOConferenceEvent daoConferenceEvent;
	
	/**
	 * The sole constructor
	 * 
	 * @param dao
	 */
	public ServiceConferenceEventImpl(DAOConferenceEvent dao)
	{
		super(dao);
		
		this.daoConferenceEvent = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.conference.ServiceConferenceEvent#traceEvent(org.ims.inv.model.conference.ModelConference, java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public void traceEvent(ModelConference conference, String eventName,
			String eventText, Integer eventState) throws ServiceException
	{
		try
		{
			if (conference != null)
			{
				ModelConferenceEvent event = new ModelConferenceEvent();
				event.setConference(conference);
				event.setEventTime(new Date());
				event.setEventName(eventName);
				event.setEventComments(eventText);
				event.setEventOperator(ContextUtil.getCurrentUser());
				event.setEventState(eventState);
				
				this.daoConferenceEvent.saveOrUpdate(event);
				
				// 更新关联
				Set<ModelConferenceEvent> events = conference.getEvents();
				if (events == null)
				{
					conference.setEvents(new HashSet<ModelConferenceEvent>());
				}
				conference.getEvents().add(event);
			}
		}
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelConferenceEvent domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelConferenceEvent.class);
		if (domain != null)
		{
			
		}
		return criteria;
	}
	
	public DAOConferenceEvent getDaoConferenceEvent()
	{
		return daoConferenceEvent;
	}

	public void setDaoConferenceEvent(DAOConferenceEvent daoConferenceEvent)
	{
		this.daoConferenceEvent = daoConferenceEvent;
	}

}
